var app = getApp()
// edit.js
Page({
    data: {
        remindPickerArray: ['不提醒'],
        remindPicker: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        this.setData({
            partyId:options.partyId,
            maxMember:options.maxMember
        })
        var newObj = {};
        this.$wuxToast = app.wux(this).$wuxToast
        app.getUserInfo(function(userInfo){
            //更新数据
            that.setData({
                userInfo:userInfo
            })
        })
        var time = options.beginTimeUnix - Math.round(new Date().getTime()/1000);
        if(time>300&&time<900){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟']
        }
        if(time>900&&time<1800){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟','提前15分钟']
        }
        if(time>1800&&time<3600){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟','提前15分钟','提前30分钟']
        }
        if(time>3600 && time<7200){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟','提前15分钟','提前30分钟','提前一小时']
        }
        if(time>7200 && time < 86400){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟','提前15分钟','提前30分钟','提前一小时','提前二小时']
        }
        if(time>86400 && time <172800){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟','提前15分钟','提前30分钟','提前一小时','提前二小时','提前一天']
        }
        if(time>172800 && time<610800){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟','提前15分钟','提前30分钟','提前一小时','提前二小时','提前一天','提前两天']
        }
        if(time>610800 ){
            newObj ['remindPickerArray'] = ['不提醒','提前5分钟','提前15分钟','提前30分钟','提前一小时','提前二小时','提前一天','提前两天','提前一周']
        }
        if(time<300&&time>5){
            newObj ['remindPickerArray'] = ['不提醒']
        }
        newObj['remindPicker'] = 0;
        this.setData(newObj);
        console.log(time)
    },

    bindPickerChange: function(e) {
        this.setData({
            remindPicker: e.detail.value
        })
    },
    listenAttenderNum:function (e) {
        if(!isNaN(e.detail.value)&&e.detail.value<100){
            this.setData({
                attenderNum:e.detail.value
            });
        }else {
            this.setData({
                attenderNum:''
            });
        }
    },
    listenDescribe:function (e) {
        this.setData({
            describe:e.detail.value
        });
    },
    attendParty:function (e) {
        var _this =this;
        var formId = e.detail.formId;
        if(this.data.attenderNum == undefined || this.data.attenderNum == '' || this.data.attenderNum < 0 ){
            _this.$wuxToast.show({
                type: 'forbidden',
                timer: 1500,
                color: '#fff',
                text: '请输入参与人数'
            });
            return false;
        }
        if(this.data.remindPicker == undefined || this.data.remindPicker == -1){
            _this.$wuxToast.show({
                type: 'forbidden',
                timer: 1500,
                color: '#fff',
                text: '请输入提醒时间'
            });
            return false;
        }
        wx.request({
            url:"https://wxapps.lovemojito.com/mApp/attendOrCancel.php",
            data: {
                attenderNum:this.data.attenderNum,
                remindPicker:this.data.remindPicker,
                description:this.data.describe,
                userInfo:this.data.userInfo,
                partyId:this.data.partyId,
                maxMember:this.data.maxMember,
                formId:formId,
                flag:1,
                jyj:'jyj'
            },
            header: {
                'content-type': 'application/json'
            },
            success:function (res) {
                console.log(res);
                if(res.data.errCode == 1){
                    _this.$wuxToast.show({
                            type: 'success',
                            timer: 1500,
                            color: '#fff',
                            text: '已完成',
                            success:function () {
                                wx.navigateTo({
                                    url: '/pages/index/index?partyId='+res.data.partyId
                                })
                            }
                })
                }else {
                    _this.$wuxToast.show({
                            type: 'cancel',
                            timer: 1500,
                            color: '#fff',
                            text: res.data.errMsg
                    })
                }
            }
        })

    }
});